
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;

/**
 * @Hisoka Build 30
 */
public class SwingEdit extends javax.swing.JFrame {

    public SwingEdit() {
        initComponents();
    }
    int pos, last_search_pos, saved_cursor_pos;
    String su;
    List array_save_text = new ArrayList();
    int revert = array_save_text.size();

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchBoxDialog = new javax.swing.JDialog();
        button_search = new javax.swing.JButton();
        field_search = new javax.swing.JTextField();
        field_replace = new javax.swing.JTextField();
        button_replace = new javax.swing.JButton();
        button_replace_all = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        checkbox_search = new javax.swing.JCheckBox();
        aboutBoxDialog = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        formatBoxDialog = new javax.swing.JDialog();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        ScrollPanel = new javax.swing.JScrollPane();
        main_text_area = new javax.swing.JTextArea();
        field_error = new javax.swing.JTextField();
        field_zeile = new javax.swing.JTextField();
        field_pos = new javax.swing.JTextField();
        MenuBar = new javax.swing.JMenuBar();
        menu_data = new javax.swing.JMenu();
        b_new = new javax.swing.JMenuItem();
        b_open = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        b_save_all = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        b_exit = new javax.swing.JMenuItem();
        menu_edit = new javax.swing.JMenu();
        b_revert = new javax.swing.JMenuItem();
        b_revert_forward = new javax.swing.JMenuItem();
        b_save = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        b_mark_all = new javax.swing.JMenuItem();
        b_search = new javax.swing.JMenuItem();
        menu_format = new javax.swing.JMenu();
        b_format = new javax.swing.JMenuItem();
        b_count_words = new javax.swing.JMenuItem();
        b_nummer = new javax.swing.JMenuItem();
        b_test = new javax.swing.JMenuItem();
        menu_help = new javax.swing.JMenu();
        b_about = new javax.swing.JMenuItem();

        searchBoxDialog.setMinimumSize(new java.awt.Dimension(400, 200));

        button_search.setText("Suchen");
        button_search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_searchActionPerformed(evt);
            }
        });

        button_replace.setText("Ersetzen");
        button_replace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_replaceActionPerformed(evt);
            }
        });

        button_replace_all.setText("Ersetze alle");
        button_replace_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_replace_allActionPerformed(evt);
            }
        });

        jLabel1.setText("Suchen nach:");

        jLabel2.setText("Ersetzen durch:");

        checkbox_search.setText("suche neustarten");
        checkbox_search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkbox_searchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout searchBoxDialogLayout = new javax.swing.GroupLayout(searchBoxDialog.getContentPane());
        searchBoxDialog.getContentPane().setLayout(searchBoxDialogLayout);
        searchBoxDialogLayout.setHorizontalGroup(
            searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchBoxDialogLayout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(field_search, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(field_replace, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(button_search, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(button_replace, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(button_replace_all, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(checkbox_search))
                .addContainerGap())
        );
        searchBoxDialogLayout.setVerticalGroup(
            searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchBoxDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(button_search)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(field_search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkbox_search))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_replace)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(searchBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_replace_all)
                    .addComponent(field_replace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        aboutBoxDialog.setMinimumSize(new java.awt.Dimension(400, 100));

        jLabel3.setText("Dieses Programm wurde erstellt von Peter Kucharski");

        jLabel4.setText("und Sebastian Schulz.");

        javax.swing.GroupLayout aboutBoxDialogLayout = new javax.swing.GroupLayout(aboutBoxDialog.getContentPane());
        aboutBoxDialog.getContentPane().setLayout(aboutBoxDialogLayout);
        aboutBoxDialogLayout.setHorizontalGroup(
            aboutBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutBoxDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(aboutBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        aboutBoxDialogLayout.setVerticalGroup(
            aboutBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutBoxDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        formatBoxDialog.setMinimumSize(new java.awt.Dimension(400, 200));

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField2");

        jTextField3.setText("jTextField3");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        jList3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList3);

        javax.swing.GroupLayout formatBoxDialogLayout = new javax.swing.GroupLayout(formatBoxDialog.getContentPane());
        formatBoxDialog.getContentPane().setLayout(formatBoxDialogLayout);
        formatBoxDialogLayout.setHorizontalGroup(
            formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(formatBoxDialogLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(63, 63, 63)
                .addGroup(formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addGroup(formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(formatBoxDialogLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(80, Short.MAX_VALUE))
        );
        formatBoxDialogLayout.setVerticalGroup(
            formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(formatBoxDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(formatBoxDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(111, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 400));

        main_text_area.setColumns(20);
        main_text_area.setFont(new java.awt.Font("Courier New", 0, 13)); // NOI18N
        main_text_area.setRows(5);
        main_text_area.setText("Hallo an alle die das hier lesen :)\nDieser Text dient als such und ersetz test.\n\nWir haben auch eine Nummerierungs Funktion die ihr hier Tesen könnt:\n\nÜberschrift\n\nÜberschrift\n\nÜberschrift\n\nIch hoffe ihr hattet viel spaß, das clit-edit Team.");
        main_text_area.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                main_text_areaCaretUpdate(evt);
            }
        });
        ScrollPanel.setViewportView(main_text_area);

        field_error.setBackground(java.awt.Color.lightGray);
        field_error.setEditable(false);

        field_zeile.setBackground(java.awt.Color.lightGray);
        field_zeile.setEditable(false);
        field_zeile.setText("Zeile: -");
        field_zeile.setAutoscrolls(false);

        field_pos.setBackground(java.awt.Color.lightGray);
        field_pos.setEditable(false);
        field_pos.setText("Pos: -");
        field_pos.setAutoscrolls(false);
        field_pos.setMaximumSize(new java.awt.Dimension(600, 200));

        menu_data.setText("Datei");

        b_new.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        b_new.setText("Neu");
        b_new.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_newActionPerformed(evt);
            }
        });
        menu_data.add(b_new);

        b_open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        b_open.setText("Öffnen");
        b_open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_openActionPerformed(evt);
            }
        });
        menu_data.add(b_open);
        menu_data.add(jSeparator1);

        b_save_all.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        b_save_all.setText("Speichern Unter");
        b_save_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_save_allActionPerformed(evt);
            }
        });
        menu_data.add(b_save_all);
        menu_data.add(jSeparator2);

        b_exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        b_exit.setText("Exit");
        b_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_exitActionPerformed(evt);
            }
        });
        menu_data.add(b_exit);

        MenuBar.add(menu_data);

        menu_edit.setText("Bearbeiten");

        b_revert.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        b_revert.setText("Rückgängig");
        b_revert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_revertActionPerformed(evt);
            }
        });
        menu_edit.add(b_revert);

        b_revert_forward.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        b_revert_forward.setText("Wiederholen");
        b_revert_forward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_revert_forwardActionPerformed(evt);
            }
        });
        menu_edit.add(b_revert_forward);

        b_save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        b_save.setText("Zwischenspeichern");
        b_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_saveActionPerformed(evt);
            }
        });
        menu_edit.add(b_save);
        menu_edit.add(jSeparator4);

        b_mark_all.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        b_mark_all.setText("Alles makieren");
        b_mark_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_mark_allActionPerformed(evt);
            }
        });
        menu_edit.add(b_mark_all);

        b_search.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        b_search.setText("Suchen & Ersetzen");
        b_search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_searchActionPerformed(evt);
            }
        });
        menu_edit.add(b_search);

        MenuBar.add(menu_edit);

        menu_format.setText("Format");

        b_format.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        b_format.setText("Schriftart");
        b_format.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_formatActionPerformed(evt);
            }
        });
        menu_format.add(b_format);

        b_count_words.setText("Wörter zählen");
        b_count_words.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_count_wordsActionPerformed(evt);
            }
        });
        menu_format.add(b_count_words);

        b_nummer.setText("Nummerierung");
        b_nummer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_nummerActionPerformed(evt);
            }
        });
        menu_format.add(b_nummer);

        b_test.setText("TestButton");
        b_test.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_testActionPerformed(evt);
            }
        });
        menu_format.add(b_test);

        MenuBar.add(menu_format);

        menu_help.setText("Hilfe");

        b_about.setText("About");
        b_about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_aboutActionPerformed(evt);
            }
        });
        menu_help.add(b_about);

        MenuBar.add(menu_help);

        setJMenuBar(MenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(field_error, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(field_pos, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(field_zeile, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(ScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 669, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(ScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(field_error, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(field_zeile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(field_pos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_searchActionPerformed
        searchBoxDialog.setSize(400, 200);
        searchBoxDialog.setVisible(true);
    }//GEN-LAST:event_b_searchActionPerformed

    private void b_aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_aboutActionPerformed
        aboutBoxDialog.setSize(400, 100);
        aboutBoxDialog.setVisible(true);
    }//GEN-LAST:event_b_aboutActionPerformed

    private void button_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_searchActionPerformed
        search();
    }//GEN-LAST:event_button_searchActionPerformed

    private void button_replaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_replaceActionPerformed
        replace();
    }//GEN-LAST:event_button_replaceActionPerformed

    private void button_replace_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_replace_allActionPerformed
        replace_all();
    }//GEN-LAST:event_button_replace_allActionPerformed

    private void b_openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_openActionPerformed
        open_folder();
    }//GEN-LAST:event_b_openActionPerformed

    private void b_mark_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_mark_allActionPerformed
        mark_all();
    }//GEN-LAST:event_b_mark_allActionPerformed

    private void b_formatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_formatActionPerformed
        formatBoxDialog.setSize(400, 200);
        formatBoxDialog.setVisible(true);
    }//GEN-LAST:event_b_formatActionPerformed

    private void b_newActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_newActionPerformed
        new_file();
    }//GEN-LAST:event_b_newActionPerformed

    private void b_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_saveActionPerformed
        save();
    }//GEN-LAST:event_b_saveActionPerformed

    private void b_revertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_revertActionPerformed
        revert_backward();
    }//GEN-LAST:event_b_revertActionPerformed

    private void b_revert_forwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_revert_forwardActionPerformed
        revert_forward();
    }//GEN-LAST:event_b_revert_forwardActionPerformed

    private void b_save_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_save_allActionPerformed
        save_under();
    }//GEN-LAST:event_b_save_allActionPerformed

    private void b_testActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_testActionPerformed
        test_button();
    }//GEN-LAST:event_b_testActionPerformed

    private void main_text_areaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_main_text_areaCaretUpdate
        saved_cursor_pos = main_text_area.getCaretPosition();
        field_pos.setText("Pos: " + saved_cursor_pos);
        field_zeile.setText("Zeile: " + StEdit.zeilenNummer(main_text_area.getText(), saved_cursor_pos));
    }//GEN-LAST:event_main_text_areaCaretUpdate

    private void checkbox_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkbox_searchActionPerformed
        search_restart();
}//GEN-LAST:event_checkbox_searchActionPerformed

    private void b_nummerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_nummerActionPerformed
        text_nummerierung();
    }//GEN-LAST:event_b_nummerActionPerformed

    private void b_exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_exitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_b_exitActionPerformed

    private void b_count_wordsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_count_wordsActionPerformed
        field_error.setText("" + StEdit.count_words(main_text_area.getText()));
    }//GEN-LAST:event_b_count_wordsActionPerformed
    //TODO Darstellung von html tabellen im editor
    //TODO Formartierung links mittig rechts blocksatz;

    //Öffnet eine beliebige datei und stellt sie als Text dar (muss noch auf texte limitiert werden)
    public void open_folder() {
        int z;
        StringBuffer temp_text = new StringBuffer("");
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        if (JFileChooser.APPROVE_OPTION == chooser.showDialog(null, "Öffnen")) {
            File dir = chooser.getSelectedFile();
            try {
                FileInputStream get_file = new FileInputStream(dir);
                try {
                    while ((z = get_file.read()) != -1) {
                        temp_text.append((char) z);
                    }
                } catch (IOException ex) {
                    field_error.setText("Beim Einlesen der Datei ist ein Fehler aufgetetten. Code: " + ex);
                }
                main_text_area.setText(temp_text.toString());
            } catch (FileNotFoundException ex) {
                field_error.setText("" + ex);
            }
        }
    }

    //Suchfunktion, sucht ab erster Position bzw. ab Maus Position
    public void search() {
        checkbox_search.setSelected(false);
        if (su == null) {
            su = "";
        }
        su = field_search.getText();
        String s = main_text_area.getText();
        pos = s.indexOf(su, saved_cursor_pos + su.length());
        saved_cursor_pos = pos;
        if (pos < 0) {
            String infoAlt = field_error.getText();
            String infoNeu = infoAlt + "Das Wort:" + su + " wurde nicht gefunden.";
            field_error.setText(infoNeu);
        } else {
            field_error.setText("Das Wort \"" + su + "\" wurde auf der Position: " + pos + " gefunden.");
            main_text_area.requestFocus();
            main_text_area.setSelectionStart(pos);
            main_text_area.setSelectionEnd(pos + su.length());
        }
    }

    //Setz die Such Position auf 0
    public void search_restart() {
        saved_cursor_pos = 0;
    }

    //ersetzt das von search makierte Wort
    public void replace() {
        main_text_area.setText(StEdit.ersetzen(main_text_area.getText(), saved_cursor_pos - field_search.getText().length(), field_search.getText(), field_replace.getText()));
    }

    //ersetz alle gefundenen Wörter
    public void replace_all() {
        main_text_area.setText(StEdit.allesErsetzen(main_text_area.getText(), pos, field_search.getText(), field_replace.getText()));
    }

    //Endhält noch fehler, erste speicherposition wird nicht wieder gegeben
    //Der durch zwischenspeicher gespeicherter Text kann wieder gegeben werden
    public void revert_backward() {
        if (revert > 0) {
            revert -= 1;
            String saved_text = array_save_text.get(revert).toString();
            main_text_area.setText(saved_text);
            field_error.setText("Revert Counter: " + revert);
        }
    }

    //Endhält noch fehler, letzte Speicherposition wird nicht wieder gegeben
    //Der durch zwischenspeicher gespeicherter Text kann wieder gegeben werden
    public void revert_forward() {
        if (revert < array_save_text.size()) {
            String saved_text = array_save_text.get(revert).toString();
            main_text_area.setText(saved_text);
            revert += 1;
            field_error.setText("Revert Counter: " + revert);
        }
    }

    //Die main_text_area wird mit einen leeren String gefüllt.
    public void new_file() {
        main_text_area.setText("");
        array_save_text.add(main_text_area.getText());
    }

    //Der zwischenspeicher für Rückgängig und Vorwärts
    public void save() {
        array_save_text.add(main_text_area.getText());
        revert = array_save_text.size();
        int array_size = array_save_text.size();
        field_error.setText("Array Größe: " + array_size);
    }

    //Erstellt eine Datei mit Inhalt des Texteditors
    public void save_under() {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        if (JFileChooser.APPROVE_OPTION == chooser.showDialog(null, "Speichern")) {
            File dir = chooser.getSelectedFile();
            try {
                FileWriter write_file = new FileWriter(dir);
                write_file.write(main_text_area.getText());
                write_file.close();
            } catch (IOException ex) {
                field_error.setText("Fehler beim speichern, Code:" + ex);
            }
        }
    }

    //Makiert den ganzen Text
    public void mark_all() {
        main_text_area.requestFocusInWindow();
        main_text_area.setSelectionStart(0);
        main_text_area.setSelectionEnd(main_text_area.getText().length());
    }

    //Nummeriert den Makierten Text teil
    public void text_nummerierung() {
        main_text_area.setText(StEdit.num_help(main_text_area.getSelectionStart(), main_text_area.getSelectionEnd(), main_text_area.getText()));
    }

    //Testbutton für neue Funktionen
    public void test_button() {
        field_error.setText("Keine Funktion zur Zeit");
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SwingEdit().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar MenuBar;
    private javax.swing.JScrollPane ScrollPanel;
    private javax.swing.JDialog aboutBoxDialog;
    private javax.swing.JMenuItem b_about;
    private javax.swing.JMenuItem b_count_words;
    private javax.swing.JMenuItem b_exit;
    private javax.swing.JMenuItem b_format;
    private javax.swing.JMenuItem b_mark_all;
    private javax.swing.JMenuItem b_new;
    private javax.swing.JMenuItem b_nummer;
    private javax.swing.JMenuItem b_open;
    private javax.swing.JMenuItem b_revert;
    private javax.swing.JMenuItem b_revert_forward;
    private javax.swing.JMenuItem b_save;
    private javax.swing.JMenuItem b_save_all;
    private javax.swing.JMenuItem b_search;
    private javax.swing.JMenuItem b_test;
    private javax.swing.JButton button_replace;
    private javax.swing.JButton button_replace_all;
    private javax.swing.JButton button_search;
    private javax.swing.JCheckBox checkbox_search;
    private javax.swing.JTextField field_error;
    private javax.swing.JTextField field_pos;
    private javax.swing.JTextField field_replace;
    private javax.swing.JTextField field_search;
    private javax.swing.JTextField field_zeile;
    private javax.swing.JDialog formatBoxDialog;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextArea main_text_area;
    private javax.swing.JMenu menu_data;
    private javax.swing.JMenu menu_edit;
    private javax.swing.JMenu menu_format;
    private javax.swing.JMenu menu_help;
    private javax.swing.JDialog searchBoxDialog;
    // End of variables declaration//GEN-END:variables
}
